package com.intct.util;

import com.intct.common.Constant;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @Author : Felix
 * @Date : 2025.05.14 15:38
 * TODO :
 **/
public class HBaseUtil {
    private static Connection connection = null;
    private static String namespace;

    static {
        try {
            String zkAddr = ParmUtil.getValue(Constant.HBASE_ZK_ADDR_KEY, "cdh-node");
            String zkPort = ParmUtil.getValue(Constant.HBASE_ZK_PORT_KEY, "2181");
            namespace = ParmUtil.getValue(Constant.HBASE_NAMESPACE_KEY, "defalut");

            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", zkAddr);
            conf.set("hbase.zookeeper.property.clientport", zkPort);
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建HBase的表
     * @param tName 表名称
     * @param family 列族名称
     * @throws IOException
     */
    public static void createTable(String tName, String family) throws IOException {
        System.out.println(family);
        // 获取 admin连接
        Admin admin = connection.getAdmin();

        // 声明TableName实例
        TableName tableName = TableName.valueOf(namespace, tName);

        // 判断是否存在
        boolean isTableExists = admin.tableExists(tableName);

        try {
            if (!isTableExists) {
                // 获取表描述
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);

                // 获取列族描述
                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));

                // 配置列族属性 == 保留版本
                columnFamilyDescriptorBuilder.setMaxVersions(3);

                tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());

                // 创建表
                admin.createTable(tableDescriptorBuilder.build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (admin != null) {
                // 关闭资源 - admin
                admin.close();
            }
        }
    }

    /**
     * 删除HBase表
     * @param tName 表名称
     */
    public static void deleteTable(String tName) throws IOException {
        // 获取 admin连接
        Admin admin = connection.getAdmin();

        // 声明TableName实例
        TableName tableName = TableName.valueOf(namespace, tName);

        // 判断是否存在
        boolean isTableExists = admin.tableExists(tableName);
        boolean isTableDisabled = admin.isTableDisabled(tableName);

        try {
            if (isTableExists) {
                if (!isTableDisabled) {
                    // 禁用表
                    admin.disableTable(tableName);
                }
                // 删除表
                admin.deleteTable(tableName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (admin != null) {
                // 关闭资源 - admin
                admin.close();
            }
        }
    }


    /**
     * 修改HBase的表
     * @param tName 表名称
     * @param oldFamily 旧列族名称
     * @param newFamily 新列族名称
     * @throws IOException
     */
    public static void aletrTable(String tName, String oldFamily, String newFamily) throws IOException {
        // 获取 admin连接
        Admin admin = connection.getAdmin();

        // 声明TableName实例
        TableName tableName = TableName.valueOf(namespace, tName);

        // 查询表
        boolean isTableExists = admin.tableExists(tableName);

        try {
            // 判断是否存在
            if (isTableExists) {
                // 1 获取旧表信息
                TableDescriptor oldTableDescriptor = admin.getDescriptor(tableName);
                // 2 获取表描述
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(oldTableDescriptor);
                // 3 新增列族
                // 3.1 获取列族描述
                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(newFamily));
                // 3.2 配置列族属性 == 保留版本
                columnFamilyDescriptorBuilder.setMaxVersions(3);
                tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());

                // 4 删除列族
                tableDescriptorBuilder.removeColumnFamily(Bytes.toBytes(oldFamily));

                // 5修改表
                admin.modifyTable(tableDescriptorBuilder.build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (admin != null) {
                // 关闭资源 - admin
                admin.close();
            }
        }
    }

    /**
     * 根据Rowkey删除一行数据
     * @param tName
     * @param rowkey
     * @throws IOException
     */
    public static void delete(String tName, String rowkey) throws IOException {
        // 获取 admin连接
        Admin admin = connection.getAdmin();

        // 声明TableName实例
        TableName tableName = TableName.valueOf(namespace, tName);

        // 获取Table连接
        Table table = connection.getTable(tableName);

        // 查询表
        boolean isTableExists = admin.tableExists(tableName);

        try {
            // 判断是否存在
            if (isTableExists) {
                Delete delete = new Delete(Bytes.toBytes(rowkey));
                table.delete(delete);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (admin != null) {
                // 关闭资源 - admin
                admin.close();
            }
        }
    }

    /**
     * 增修数据
     * @param tName 表名称
     * @param put put实例
     * @throws IOException
     */
    public static void put(String tName, Put put) throws IOException {
        // 获取 admin连接
        Admin admin = connection.getAdmin();

        // 声明TableName实例
        TableName tableName = TableName.valueOf(namespace, tName);

        // 获取Table连接
        Table table = connection.getTable(tableName);

        // 查询表
        boolean isTableExists = admin.tableExists(tableName);

        try {
            // 判断是否存在
            if (isTableExists) {
                table.put(put);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (admin != null) {
                // 关闭资源 - admin
                admin.close();
            }
        }
    }

    public static Result getRowKey(String tName, String rowkey) throws IOException {
        // 获取 admin连接
        Admin admin = connection.getAdmin();

        // 声明TableName实例
        TableName tableName = TableName.valueOf(namespace, tName);

        // 获取Table连接
        Table table = connection.getTable(tableName);

        // 查询表
        boolean isTableExists = admin.tableExists(tableName);

        try {
            // 判断是否存在
            if (isTableExists) {
                Get get = new Get(Bytes.toBytes(rowkey));
                return table.get(get);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (admin != null) {
                // 关闭资源 - admin
                admin.close();
            }
        }
        return null;
    }
}
